package com.kandinfo.base.security.authc;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.kandinfo.base.common.util.web.WebUtil;
import com.kandinfo.base.security.SecurityContext;
import com.kandinfo.base.security.dto.MenuDto;
import com.kandinfo.base.security.service.RoleService;
import com.kandinfo.base.security.util.RoleConstants;

public class ACLInterceptor extends HandlerInterceptorAdapter {
	public static final String DEFAULT_PARAM_NAME = "menu";
	@Autowired
	private RoleService roleService;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
		if (!SecurityContext.getInstance().isAuthenticated()// 未授权
			|| handler == null || !handler.getClass().equals(org.springframework.web.method.HandlerMethod.class)
			|| SecurityContext.getInstance().getCurrentUser() == null) {
			return true;
		}
//		if (SecurityContext.getInstance().isAuthenticated() && handler != null && handler.getClass().equals(HandlerMethod.class) && null != SecurityContext.getInstance().getCurrentUser() ) {
//			HandlerMethod method = (HandlerMethod) handler;
//			Menu ann = 	method.getMethodAnnotation(Menu.class);
				
//			Long id = SecurityContext.getInstance().getCurrentUser().getId();
//			List<MenuDto> newMenuList = null;
//			if(newMenuList == null){
//				List<MenuDto> menuListQueyr = roleService.findAllMenu();
//				if(!SecurityContext.getInstance().hasRoles(RoleConstants.SUPER_ADMIN)){
//					newMenuList = menuListQueyr;
//				}else{
//					newMenus = new ArrayList<MenuDto>();
//					for (MenuDto menu : menuListQueyr) {
//						if (!removeMenu(menuListQueyr)) {
//							newMenus.add(menuListQueyr);
//						}
//					}
//				}
//			}
		request.setAttribute("menu", getMenu());
//		}
		return true;
	}
	
	/**
	 * 过滤菜单
	 * @param menuList
	 */
	private List<MenuDto> getMenu() {
		List<MenuDto> menuList = roleService.findAllMenu();
		if (CollectionUtils.isEmpty(menuList)) {
			return menuList;
		}
		List<MenuDto> newMenuList = new ArrayList<MenuDto>();
		if(!SecurityContext.getInstance().hasRoles(RoleConstants.SUPER_ADMIN)){// 超级管理员不通过权限过滤菜单
			for (MenuDto m : menuList) {
				newMenuList.add(removeMenuByPrem(m));
			}
			menuList.clear();
			menuList.addAll(newMenuList);
		}
			 
		newMenuList = new ArrayList<MenuDto>();
		String device = WebUtil.checkDeviceType();
		for (MenuDto m : menuList) {
			if (!device.equalsIgnoreCase(m.getDevice())) {
				continue;
			}
			newMenuList.add(removeByDevice(m, device));
		}
		return newMenuList;
	}

	/**
	 * 通过设备过滤菜单
	 * @param root
	 * @return
	 */
	private MenuDto removeByDevice(MenuDto root, String device) {
		if(SecurityContext.getInstance().hasRoles(RoleConstants.SUPER_ADMIN)){
			return root;
		}
		if (CollectionUtils.isEmpty(root.getChilds())) {
			return root;
		}
		List<MenuDto> childs = new ArrayList<MenuDto>();
		for (MenuDto m : root.getChilds()) {
			if (device.equalsIgnoreCase(m.getDevice())) {
				childs.add(m);
				removeByDevice(m, device);
			}
		}
		root.setChilds(childs);
		return root;
	}
	
	/**
	 * 通过权限过滤菜单
	 * @param menuMap
	 */
	private MenuDto removeMenuByPrem(MenuDto root) {
		if (CollectionUtils.isEmpty(root.getChilds())) {
			return root;
		}
		List<MenuDto> childs = new ArrayList<MenuDto>();
		for (MenuDto m : root.getChilds()) {
			if (SecurityUtils.getSubject().isPermitted(m.getPermName())) {
				childs.add(m);
				removeMenuByPrem(m);
			}
		}
		root.setChilds(childs);
		return root;
	}
	


}
